package com.itheima.test;

import org.activiti.engine.*;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

public class ActivitiBusinessDemo {

    /**
     * 添加业务key到Activiti的表
     */
    @Test
    public void addBusinessKey(){
        //1、获取流程引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        //2、获取RuntimeService
        RuntimeService runtimeService = processEngine.getRuntimeService();

        //3、启动流程的过程中添加BusinessKey
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myEvection", "1001");

        //4、输出
        System.out.println("processInstance.getBusinessKey() = " + processInstance.getBusinessKey());
    }

    /**
     * 全部流程实例的挂起和激活
     */
    @Test
    public void suspendAllProcess(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = processEngine.getRepositoryService();

        //查询流程定义信息
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionKey("myEvection")
                .singleResult();

        //获取当前流程定义的实例都是挂起的状态
        boolean suspended = processDefinition.isSuspended();

        //获取流程定义的id
        String processDefinitionId = processDefinition.getId();


        if (suspended) {
            //如果是挂起，改为激活
            repositoryService.activateProcessDefinitionById(processDefinitionId,true,null);
            System.out.println("流程定义id" + processDefinitionId + "已激活");
        }else {
            //如果是激活，改为挂起
            repositoryService.suspendProcessDefinitionById(processDefinitionId,true,null);
            System.out.println("流程定义id" + processDefinitionId + "已挂起");
        }


    }

    /**
     * 单个流程实例的挂起和激活
     */
    @Test
    public void suspendSingleProcess(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = processEngine.getRuntimeService();

        //查询流程实例
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
                .processInstanceId("7501")
                .singleResult();


        //获取流程实例的状态
        boolean suspended = processInstance.isSuspended();

        //获取流程实例的id
        String processInstanceId = processInstance.getId();


        if (suspended) {
            //如果是挂起，改为激活
            runtimeService.activateProcessInstanceById(processInstanceId);
            System.out.println("流程实例id" + processInstanceId + "已激活");
        }else {
            //如果是激活，改为挂起
            runtimeService.suspendProcessInstanceById(processInstanceId);
            System.out.println("流程实例id" + processInstanceId + "已挂起");
        }


    }

    /**
     * 尝试完成挂起的任务 ActivitiException: Cannot complete a suspended task
     */
    @Test
    public void testCompleteZhangsanSuspendTask(){
        //1、创建ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        //2、获取TaskService
        TaskService taskService = processEngine.getTaskService();

        //3、查询jerry-myEvection任务
        Task task = taskService.createTaskQuery()
                .processInstanceId("7501")
                .taskAssignee("zhangsan")
                .singleResult();
        System.out.println("流程实例ID=" + task.getProcessInstanceId());
        System.out.println("任务ID=" + task.getId());
        System.out.println("任务负责人=" + task.getAssignee());
        System.out.println("任务名称=" + task.getName());

        //4、完成任务
        taskService.complete(task.getId());//ActivitiException: Cannot complete a suspended task
    }


}
